package com.threeonemeeting.activity;

import java.util.Map;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.task.RegisterTask;

public class RegisterActivity extends BaseTabActivity implements ICallBack {
	private ProgressDialog loadingDialog;

	private String strJumpTo = "MainMy31Activity"; // 需要跳转到的目的Activity
	private Bundle b = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_register);
		init("用户注册");

		b = this.getIntent().getExtras();
		if (b != null) {
			strJumpTo = b.getString("jumpto");
			if (strJumpTo == null) {
				strJumpTo = "MainMy31Activity";
			}
		}

		final EditText editAccount = (EditText) findViewById(R.id.editAccount);
		editAccount.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {

				String strHint = getResources().getString(
						R.string.string_nickname_hint);
				String strAccount = editAccount.getText().toString();

				if (hasFocus == true) {
					if (strAccount.equalsIgnoreCase(strHint) == true) {
						editAccount.setText("");
					}
				} else {

					if (strAccount.equalsIgnoreCase("") == true) {
						editAccount.setText(strHint);
					}
				}
			}

		});

		final EditText editEmail = (EditText) findViewById(R.id.editEmail);
		editEmail.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_email_hint);
				String strEmail = editEmail.getText().toString();

				if (hasFocus == true) {
					if (strEmail.equalsIgnoreCase(strHint) == true) {
						editEmail.setText("");
					}
				} else {

					if (strEmail.equalsIgnoreCase("") == true) {
						editEmail.setText(strHint);
					}
				}
			}
		});

		final EditText editPassword = (EditText) findViewById(R.id.editPassword);
		editPassword.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_password_hint);
				String strPassword = editPassword.getText().toString();

				if (hasFocus == true) {
					if (strPassword.equalsIgnoreCase(strHint) == true) {
						editPassword.setText("");
					}
				} else {
					if (strPassword.equalsIgnoreCase("") == true) {
						editPassword.setText(strHint);
					}
				}
			}
		});

		final EditText editPasswordEnsure = (EditText) findViewById(R.id.editPasswordEnsure);
		editPasswordEnsure
				.setOnFocusChangeListener(new OnFocusChangeListener() {

					public void onFocusChange(View v, boolean hasFocus) {

						String strHint = getResources().getString(
								R.string.string_password_ensure_hint);
						String strPassword = editPasswordEnsure.getText()
								.toString();

						if (hasFocus == true) {
							if (strPassword.equalsIgnoreCase(strHint) == true) {
								editPasswordEnsure.setText("");
							}
						} else {
							if (strPassword.equalsIgnoreCase("") == true) {
								editPasswordEnsure.setText(strHint);
							}
						}
					}
				});

		// 注册按钮
		Button btnRegister = (Button) findViewById(R.id.btn_register);
		OnClickListener hotMeetingBtnClickListener = new OnClickListener() {

			public void onClick(View v) {
				// 检查nick name是否为空
				String strNickName = editAccount.getText().toString();
				if (strNickName.trim().equals("") == true) {
					MessageBox.showToast(
							RegisterActivity.this.getApplicationContext(),
							"昵称不能为空");
					editAccount.setFocusable(true);
					editAccount.setFocusableInTouchMode(true);
					editAccount.requestFocus();
					return;
				}

				// 检查电子邮件是否为空
				String strEmail = editEmail.getText().toString();
				if (strEmail.trim().equals("") == true) {
					MessageBox.showToast(
							RegisterActivity.this.getApplicationContext(),
							"Email不能为空");

					editEmail.setFocusable(true);
					editEmail.setFocusableInTouchMode(true);
					editEmail.requestFocus();

					return;
				}

				if (MiscHelper.isEmail(strEmail) == false) {
					MessageBox.showToast(
							RegisterActivity.this.getApplicationContext(),
							"请输入有效的Email地址");

					editEmail.setFocusable(true);
					editEmail.setFocusableInTouchMode(true);
					editEmail.requestFocus();

					return;
				}

				// 检查密码区域
				String strPassword1 = editPassword.getText().toString();
				String strPassword2 = editPasswordEnsure.getText().toString();

				if (strPassword1.trim().equals("") == true) {
					MessageBox.showToast(
							RegisterActivity.this.getApplicationContext(),
							"密码不能为空");

					editEmail.setFocusable(true);
					editEmail.setFocusableInTouchMode(true);
					editEmail.requestFocus();

					return;
				}

				if (strPassword1.equals(strPassword2) == false) {
					MessageBox.showToast(
							RegisterActivity.this.getApplicationContext(),
							"两次密码输入不一致");

					editEmail.setFocusable(true);
					editEmail.setFocusableInTouchMode(true);
					editEmail.requestFocus();

					return;
				}

				MultiActivityTab parent = (MultiActivityTab) RegisterActivity.this
						.getParent();
				loadingDialog = MessageBox
						.showProcessDlg(parent, "正在注册，请稍候...");
				loadingDialog.show();

				// 校验合法，发送请求
				RegisterTask task = new RegisterTask(RegisterActivity.this,
						RegisterActivity.this);
				Bundle params = new Bundle();
				params.putString("email", strEmail);
				params.putString("nickName", strNickName);
				params.putString("password1", strPassword1);
				params.putString("password2", strPassword2);
				TaskManager.addTask(task, params, true);
			}
		};
		btnRegister.setOnClickListener(hotMeetingBtnClickListener);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			exitConfirmDialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	protected void exitConfirmDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确定要退出注册吗?");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	public void doCallBack(Map<String, Object> mapData) {

		if (loadingDialog != null) {
			loadingDialog.dismiss();
		}

		Integer msgType = (Integer) mapData.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {

			String strErrorMsg = (String) mapData.get(HttpUtil.KEY_ERROR_MSG);
			if (strErrorMsg != null) {
				MessageBox.showToast(this, strErrorMsg);
			}

			return;
		}

		if (msgType == HttpConst.TASKTYPE_REGISTER) {

			MessageBox.showToast(this, "祝贺您注册成功");
			startActivity("", strJumpTo);
		}
	}
}
